package com.msmall.community.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.OnlineUser;
import com.msmall.community.team.model.Team;
import com.msmall.community.team.model.TeamProperty;
import com.msmall.community.team.service.TeamManager;

public class TeamSharedTeamTag extends TagSupport{
	
	private static final long serialVersionUID = 5519842521452264132L;

	private final Log log = LogFactory.getLog(TeamSharedTeamTag.class);
	
	private Long teamid;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		TeamManager manager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		JspWriter writer = pageContext.getOut();
		StringBuffer buffer = new StringBuffer();
		buffer.append("<ul>");
		try{
			List<Team> list = null;
			TeamProperty property = manager.getSharedTeam(teamid);
			if(property != null){
				String value = property.getPropertyValue();
				if(value != null){
					String[] ids = value.split(",");
					for(int i=0; i<ids.length; i++){
						try{
							Long tid = Long.valueOf(ids[i].trim());
							Team team = (Team) manager.get(Team.class, tid);
							if(team != null){
								if(team.getStatus() != Team.STATUS_NORMAL.intValue())
									continue;
								if(list == null)
									list = new ArrayList<Team>();
								list.add(team);
							}
						}catch(Exception e){
							continue;
						}
					}
				}
			}
			if(list == null || list.size() == 0){
				buffer.append("<li>").append("暂无盟友").append("</li>");
			}else{
				for(int i=0; i< list.size(); i++){
					Team team = list.get(i);
					buffer.append("<li><a href='" + getTeamIndexUrl(team) + "'>").append(team.getName()).append("</a></li>");
				}
			}
			buffer.append("</ul>");
			
		}catch(Exception e){
			log.error(e);
		}
		try {
			writer.print(buffer.toString());
		} catch (IOException e) {
			log.error(e);
		}
		return (SKIP_BODY);
	}
	
	private String getUserAgentType(HttpSession session){
		try{
			return OnlineUser.getOnlineUsers().get(getOnlineUserid(session)).getUserAgentBean().getUserAgentType();
		}catch(Exception e){
			return Constants.USER_AGENT_TYPE_HTML;
		}
	}
	
	private String getTeamIndexUrl(Team team){
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse res = (HttpServletResponse) super.pageContext.getResponse();
		HttpSession session = req.getSession();
		String url = "";
		if(Constants.USER_AGENT_TYPE_WML.equalsIgnoreCase(getUserAgentType(session)))
			url = ConstantUrl.WML_TEAM_INDEX +"?teamid="+team.getTeamid();
		else
			url = ConstantUrl.HTML_TEAM_INDEX +"?teamid="+team.getTeamid();
		url = req.getContextPath() + url;
		return res.encodeURL(url);
	}
	
	private Long getOnlineUserid(HttpSession session){
		return (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
	}
	public Long getTeamid() {
		return teamid;
	}

	public void setTeamid(Long teamid) {
		this.teamid = teamid;
	}
}
